﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _07Cipher
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter text:");
            string str = Console.ReadLine();
            Console.WriteLine("Enter key:");
            string key = Console.ReadLine();
            string encryptedStr = EncryptString(str, key);
            Console.WriteLine("Encrypted message:");
            foreach (var item in encryptedStr)
            {
                Console.Write(item);
            }
            Console.WriteLine();
            Console.WriteLine("Back to original message:\r\n{0}", DecryptString(EncryptString(str, key),key));
        }
        static string EncryptString(string str, string key)
        {
            StringBuilder result = new StringBuilder();
            for (int i = 0; i < str.Length; i++)
            {
                result.Append((char)(str[i] ^ key[i % key.Length]));
            }
            return result.ToString();
        }
        static string DecryptString(string str, string key)
        {
            return EncryptString(str, key);
        }
    }
}
